/**
 * @param {number[]} nums
 * @return {number}
 */
var maxProduct = function (nums) {
  if (nums.length === 0) return 0

  //局部最大值和最小值
  let maxendingHere = nums[0]
  let minendingHere = nums[0]
  //全局最大值
  let maxsofar = nums[0]

  for (let i = 1; i < nums.length; i++) {
    //更新局部最大值、最小值
    let temp = nums[i]
    let tempMax = Math.max(temp, temp * maxendingHere, temp * minendingHere)//临时存储的局部最大值
    minendingHere = Math.min(temp, temp * maxendingHere, temp * minendingHere)
    maxendingHere = tempMax//因为此时minendingHere已经发生改变，所以用临时存储的局部最大值
    //更新全局最大值
    maxsofar = Math.max(maxendingHere, maxsofar)
  }

  return maxsofar
};